<?php
/**
	Classes:
	order
	order::delivery
	order::delivery::shipping_type
	order::payment
	order::payment::payment_method
 */

class order extends myDbTb{
	public $id;
	public $items=array();
	public $subTotal;
	public $price;
	public $delivery;
	public $payment;
	public $comments;
	public $phone;
	public $sec_phone;
	public $status=1;
	public $status_date;
	public $payment_method_id;
	public $shipping_type_id;
	public $user_id;
	public $address_id;

	/**
	 * It inserts order data to database
	 * It returns false if the data has not been inserted and sets attribute id to the inserted id.
	 */
	function db_insert() {
		$sql = "INSERT INTO "
				. TABLE_ORDERS
			. " ("
				. " user_id"
				. " , address_id"
				. " , phone"
				. " , sec_phone"
				. " , payment_method_id"
				. " , status_date"
				. " , shipping_type_id"
				. " , status"
				. " , comments"
				. " , price"
			. " ) VALUES ("
				. $this->user_id
				. " , " . $this->address_id
				. " , '" . $this->phone . "'"
				. " , '" . $this->sec_phone . "'"
				. " , " . $this->payment->method->id
				. " , NOW()"
				. " , " . $this->delivery->shipping_type->id
				. " , '" . $this->status . "'"
				. " , '" . $this->comments . "'"
				. " , " . $this->price
			. " )";
			
		return $this->sql_request($sql);
	}
	/*
	 * It loads order data from DB
	 * It returns false if the data has not been loaded.
	 */
	function db_load() {
		$sql = "SELECT o.* FROM " .
	  		TABLE_ORDERS . " o " .
	  		" where o.id = ".$this->id;
		return $this->sql_request($sql);
	}
	
	// It generates the list of items as order_items and loads them from the database
	function db_load_items()  {
		global $server;
		$sql = "SELECT op.* FROM " .
				TABLE_ORDERS_PRODUCTS . " op " .
				" where op.order_id = ".$this->id;

		$result = $server->db->link->query($sql);
		$load_check = ($result->num_rows > 0);
		if ($load_check) {
			while ($row = $result->fetch_assoc()) {
				$order_item= new order_item();
				$order_item->set_from_array($row);
				array_push($this->items,$order_item);
			}
			return $load_check;
		}
	}
	  /*
	   * For changing order status
	   */
	  function db_update_status() {
		#Hacemos la confirmación
			$sql = "update " . 
			TABLE_ORDERS .
			" set".
			" status=" . $this->status .
			" ,status_date=" . "NOW()" .
			" where id=" . $this->id .
			" LIMIT 1";
			return $this->sql_request($sql);
	  }

	/*
	 * Remove the order from the DB. It only works when the order has not been sent yet,
	 * that is, the order status is <2. Only order administrator could delete it at this point.
	 */
	function db_delete() {
		// Prevents for deleting order when order has been sent
		if ( !$this->id || ($this->status > 1) ) return false; 
	
		$sql="DELETE FROM ".
		TABLE_ORDERS .
		" WHERE id=" . $this->id .
		" LIMIT 1";
		return $this->sql_request($sql);
	}
	/*
	 * Remove the order from the DB. It only works when the order has not been sent yet,
	 * that is, the order status is <2. Only order administrator could delete it at this point.
	 */
	function db_delete_items() {
		// Prevents for deleting order when order has been sent
		if ( !$this->id || ($this->status > 1) ) return false; 
	
		$sql="DELETE FROM ".
		TABLE_ORDERS_PRODUCTS .
		" WHERE order_id=" . $this->id;

		return $this->sql_request($sql);
	}
	/*
	 * It gets the last order purchased by the customer
	 * that commits $status condition for the order status.
	 * It returns the order object
	 */
	function db_get_last($status_condition) {
		global $server;
		$sql = "SELECT DISTINCT o.id FROM " .
	  		TABLE_ORDERS . " o " .
			" WHERE o.user_id = " . $this->user_id .
			" AND o.status ".$status_condition.
			" ORDER BY o.status_date DESC LIMIT 1";

		return $this->sql_request($sql);
	}
}
/*
 * Esta clase es complementaria a order
 * Agrupa los datos y funciones relaccionadas con el envío
 */
class delivery {
	public $shipping_type; //tipo de transporte
	public $address;
	public $phone;
	public $sec_phone;
	public $shipping_types;
	
	/*
	 * It loads te available shipping Types
	 */
	function db_load_shipping_types() {
		global $server;
		$sql = "SELECT s.* FROM " .
	  		TABLE_SHIPPING_TYPES . " s" .
	  		" where 1";
		$result = $server->db->link->query($sql);
		if ($result->num_rows > 0) $this->shipping_types=array();
		for($i=0;$i<$result->num_rows;$i++) {
			$row = $result->fetch_array();
			$shippingType=new shipping_type();
			$shippingType->set_from_array($row);
			array_push($this->shipping_types, $shippingType);
		}
	}
}

class shipping_type extends myDbTb{
	public $id;
	public $price;
	public $name;
	public $description;

	/*
	 * It loads
	 */
	function db_load() {
		$sql = "SELECT s.* FROM " .
	  		TABLE_SHIPPING_TYPES . " s" .
	  		" where s.id = ".$this->id;

		return $this->sql_request($sql);
	}
}

class payment_method extends myDbTb {
	public $id;
	public $price;
	public $name;

	function db_load() {
		$sql = "SELECT p.* FROM " .
	  		TABLE_PAYMENT_METHODS . " p" .
	  		" where p.id = ".$this->id;
		return $this->sql_request($sql);
	}
}
class payment {
	public $method;
	public $methods;
	/*
	 * It loads te available payment methods
	 */
	function db_load_methods() {
		global $server;
		$sql = "SELECT p.* FROM " .
	  		TABLE_PAYMENT_METHODS . " p" .
	  		" where 1";
		$result = $server->db->link->query($sql);
		if ($result->num_rows > 0) $this->methods=array();
		for($i=0;$i<$result->num_rows;$i++) {
			$row = $result->fetch_array();
			$method=new payment_method();
			$method->set_from_array($row);
			array_push($this->methods, $method);
		}
	}
}

?>